package ch.fhnw.qualiscan.api.output;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.ApiModel;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainDescription;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;

@XmlRootElement(name = "domain")
@XmlType(propOrder = {"link", "id", "text"})
public class DomainOutput {

	Domain domain;
	
	public DomainOutput() {}
	
	public DomainOutput(Domain domain) {
		this.domain = domain;
	}
	
	@XmlElement
    public int getId() {
        return this.domain.getId();
    }
	
	@XmlElementWrapper(name="texts")
	public List<LanguageOutput> getText() {
		List<LanguageOutput> translations = new ArrayList<LanguageOutput>();
		Iterator domainIterator = domain.getDomaintexts().iterator();
		
		ApiModel apiModel = new ApiModel();
		
		while(domainIterator.hasNext()) {
			DomainText domainText = (DomainText) domainIterator.next();
			// get domain description if it exists
			DomainDescription dD = apiModel.getDomainDescription(domain, domainText.getLanguage());
			translations.add(new LanguageOutput(domainText.getText(), dD.getText(), domainText.getLanguage()));
		}
		
		return translations;
	}
	
	@XmlElement String getLink() {
		return Config.API_URL + "domains/" + this.domain.getId();
	}

}
